package GUI;

import java.awt.Color;
import java.awt.HeadlessException;

import javax.swing.JOptionPane;

import Client_logic.ClientNotInitializesException;
import Client_logic.ConnectionToServer;
import Client_logic.Category;

/**
 * This class use for update and save category.
 * @author ido
 * @author michael
 * @author sharon
 * @author matan
 */
public class CategoryUpdateForm extends TypeManagementForm {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private boolean addflag;
	private boolean updateflag;
	private boolean removeflag;
	private Category category = null; 
	
	public CategoryUpdateForm(){
		super();
		initialize(); 
	}
	
	/**
	 * This method initializes CustomerForm
	 */
	private void initialize() {
		category = new Category(0,"");		
		addflag = false;
		updateflag = false;
		removeflag = false;
		updateSectionsIDComboBox();
		getIdLabel().setText("Category ID");
		getNameLabel().setText("Category name");
		getTypesIDLabel().setText("All type in category");
		getProductsIDLabel().setText("All product in type");
		getAddbuttonLabel().setText("Selected category info:");
		getNameTextField().setEditable(false);
		getIdTextField().setEditable(false);
		addSaveButton();
		addUpdateButton();
		addremoveButton();
		addTypesIDComboBox();
		addSectionsIDComboBox();		
	}
	
	/**
	 * This method update form comboBoxes to data from data base
	 */
	public void updateForm(){
		updateSectionsIDComboBox();
	}
	
	/**
	 * This method initializes sectionsIDComboBox ActionListener
	 */
	private void addSectionsIDComboBox(){
		getSectionsIDComboBox().addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {	
				int sectionID = 0;
				String str = (String)getSectionsIDComboBox().getSelectedItem();
				if (str.equals(":"))
					return;
				try{
					sectionID = Integer.parseInt(str.substring(0, str.indexOf(":")));
				}
				catch( NumberFormatException exc){
					return;
				}
				if (sectionID > 0){
					getIdTextField().setText(String.valueOf(sectionID));
					getNameTextField().setText(str.substring(str.indexOf(":")+1, str.length()));
				}
				else{
					getIdTextField().setText("");
					getNameTextField().setText("");
					return;
				}
				category.SetCategoryID(sectionID);
				String[] typelist = null;
				try{
					int i=0,itemcount = getTypesIDComboBox().getItemCount();
					if (itemcount > 0) {
						getTypesIDComboBox().addItem(":");
						getTypesIDComboBox().setSelectedItem(":");				
					}
					for (i=0; i < itemcount ; i++ ){				
						getTypesIDComboBox().removeItemAt(0);						
					}			
					typelist = category.GetAllCategoryTypes();
					if (typelist != null){
						int len = typelist.length;
						for(i=0; i < len; i++){
							getTypesIDComboBox().addItem(typelist[i]);							
						}						
					}	
				}
				catch(ClientNotInitializesException exc){
					JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);
				}
				catch(Exception except){
					JOptionPane.showMessageDialog(null, "SYSTEM ERROR!!!","",JOptionPane.ERROR_MESSAGE);
				}				
			}
		});	
	}
	
	/**
	 * This method initializes typesIDComboBox
	 */
	private void addTypesIDComboBox(){
		getTypesIDComboBox().addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {			
				String str = (String)getTypesIDComboBox().getSelectedItem();
				if (str.equals(":")){
					getProductsIDTextArea().setText("");
					return;
				}
				String typeID = str.substring(0, str.indexOf(":"));
				String[] productslist = null;
				try{
					ConnectionToServer connectiontoserver = new ConnectionToServer();		
					productslist = (String[]) connectiontoserver.loadTypeProduct
					(Integer.parseInt(typeID));
					if (productslist == null){
						getProductsIDTextArea().setText("");
					}							
					else{
						String tmpstr = "";
						int len = productslist.length;
						for(int i=0; i < len; i++){
							tmpstr = tmpstr+(productslist[i]+"\n");
						}
						getProductsIDTextArea().setText(tmpstr);
					}
				}
				catch(ClientNotInitializesException exc){
					JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);
				}
				catch(Exception except){
					JOptionPane.showMessageDialog(null, "SYSTEM ERROR!!!","",JOptionPane.ERROR_MESSAGE);
				}
			}
		});		
	}
	
	/**
	 * This method initializes SaveButton ActionListener
	 */
	private void addSaveButton(){
		getSaveButton().addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {			
				if (addflag){
					saveSection();										
				}
				else{
					getSaveButton().setText("OK");
					getAddbuttonLabel().setText("PLZ enter category ID and Name:");
					getNameTextField().setEditable(true);
					getIdTextField().setEditable(true);
					getUpdateButton().setEnabled(false);
					getRemoveButton().setEnabled(false);
					getIdTextField().setForeground(Color.red);
					getNameTextField().setForeground(Color.red);
					getAddbuttonLabel().setForeground(Color.red);
					addflag = true;
				}
			}
		});		
	}
	
	/**
	 * This method initializes updateButton ActionListener
	 */
	private void addUpdateButton(){
		getUpdateButton().addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {			
				if (updateflag){
					updateSection();	
				}
				else{
					getUpdateButton().setText("OK");
					getAddbuttonLabel().setText("PLZ enter new category name:");
					getNameTextField().setEditable(true);
					getRemoveButton().setEnabled(false);
					getSaveButton().setEnabled(false);
					getIdTextField().setForeground(Color.red);
					getNameTextField().setForeground(Color.red);
					getAddbuttonLabel().setForeground(Color.red);
					updateflag = true;
				}
			}
		});		
	}
	
	/**
	 * This method update selected category
	 */
	private void updateSection(){
		int sectionID = 0;
		try{
			sectionID = Integer.parseInt(getIdTextField().getText());
		}
		catch(NumberFormatException exception){
			return;
		}					
		String sectionName = getNameTextField().getText();
		if (sectionID > 0 && !sectionName.equals("")){
			category.SetCategoryID(sectionID);
			category.SetCategoryName(sectionName);
			try {
				if (category.updateCategory()){
					String oldname = (String)getSectionsIDComboBox().getSelectedItem();
					oldname = oldname.substring(oldname.indexOf(":")+1, oldname.length());
					getSectionsIDComboBox().removeItem(sectionID+":"+oldname);
					getSectionsIDComboBox().addItem(sectionID+":"+sectionName);
					JOptionPane.showMessageDialog(null, "Category successfuly update");
					restore();			
				}
				else
					JOptionPane.showMessageDialog(null, "Category not update!!!","",JOptionPane.WARNING_MESSAGE);
			} 
			catch (ClientNotInitializesException e1) {
				JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);							
			}						
		}
		else
			JOptionPane.showMessageDialog(null, "PLZ enter category name!","",JOptionPane.WARNING_MESSAGE);
	}
	
	
	/**
	 * This method add entered category to data base
	 */
	private void saveSection(){
		int sectionID = 0;
		try{
			sectionID = Integer.parseInt(getIdTextField().getText());
		}
		catch(NumberFormatException exception){
			JOptionPane.showMessageDialog(null, "Ilegal ID","",JOptionPane.WARNING_MESSAGE);
			return;
		}					
		String sectionName = getNameTextField().getText();
		if (sectionID > 0 && !sectionName.equals("")){
			category.SetCategoryID(sectionID);
			category.SetCategoryName(sectionName);
			try{
				if (category.addCategory()){
					JOptionPane.showMessageDialog(null, "Category successfuly saved");
					getSectionsIDComboBox().addItem(sectionID+":"+sectionName);
					restore();
				}
				else
					JOptionPane.showMessageDialog(null, "Category not saved!!!","",JOptionPane.WARNING_MESSAGE);
			} 
			catch (ClientNotInitializesException e1) {
				JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);							
			}						
		}
		else
			JOptionPane.showMessageDialog(null, "Ilegal input!","",JOptionPane.WARNING_MESSAGE);		
	}
	
	/**
	 * This method initializes removeButton ActionListener
	 */
	private void addremoveButton(){
		getRemoveButton().addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {	
				if (removeflag){
					removeSection();	
					restore();
				}
				else{
					getRemoveButton().setText("OK");
					getAddbuttonLabel().setText("Remove this category:");
					getUpdateButton().setEnabled(false);
					getSaveButton().setEnabled(false);
					getIdTextField().setForeground(Color.red);
					getNameTextField().setForeground(Color.red);
					getAddbuttonLabel().setForeground(Color.red);
					removeflag = true;
				}
			}
		});		
	}
	
	/**
	 * This method remove selected category from data base
	 */
	private void removeSection(){
		int sectionID = 0;
		try{
			sectionID = Integer.parseInt(getIdTextField().getText());
		}
		catch(NumberFormatException exception){
			return;
		}		
		category.SetCategoryID(sectionID);
		try{
			if (category.removeCategory()){
				getSectionsIDComboBox().removeItem(sectionID+":"+getNameTextField().getText());
				JOptionPane.showMessageDialog(null, "Category successfuly remove");				
			}
			else
				JOptionPane.showMessageDialog(null, "Category not remove!!!","",JOptionPane.WARNING_MESSAGE);			
		} 
		catch (HeadlessException e1) {
			JOptionPane.showMessageDialog(null, "System ERROR!!!","",JOptionPane.ERROR_MESSAGE);							
			
		} 
		catch (ClientNotInitializesException e1) {
			JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);							
		}		
	}
	
	/**
	 * This method restore the form button  
	 */
	public void restore(){
		getSaveButton().setText("Add new");	
		getUpdateButton().setText("UPDATE");	
		getRemoveButton().setText("Remove");
		getAddbuttonLabel().setText("Selected category info:");	
		addflag = false;
		updateflag = false;
		removeflag = false;
		getIdTextField().setForeground(Color.black);
		getNameTextField().setForeground(Color.black);
		getAddbuttonLabel().setForeground(Color.black);
		getNameTextField().setEditable(false);
		getIdTextField().setEditable(false);
		getIdTextField().setText("");
		getNameTextField().setText("");
		getUpdateButton().setEnabled(true);
		getRemoveButton().setEnabled(true);
		getSaveButton().setEnabled(true);
	}
}
